package net.wds.ssbus.modules.ssbus.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import net.wds.ssbus.common.annotation.SsbusLog;
import net.wds.ssbus.common.utils.R;
import net.wds.ssbus.modules.dict.entity.TbBaseDictitemEntity;
import net.wds.ssbus.modules.ssbus.pojo.TbSsbusRouteEntrty;
import net.wds.ssbus.modules.ssbus.pojo.TbSsbusTaskEntrty;
import net.wds.ssbus.modules.ssbus.service.TbSsbusRouteService;
import net.wds.ssbus.modules.ssbus.service.TbSsbusTaskService;
import org.quartz.*;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/tbssbustask")
public class TbSsbusTaskController {

    /**
     * @author zl
     * @date 2020/10/31
     */
    @Resource
    private TbSsbusTaskService tbSsbusTaskService;

    @Resource
    private TbSsbusRouteService tbSsbusRouteService;
    /**
     *
     * @param key 查询条件
     * @param page 分页数量
     * @param limit 分页大小
     * @return
     * @throws SchedulerException
     */
    @GetMapping("/list")
    @ApiOperation(value = "定时任务_获取", notes = "获取")
    @ResponseBody
    public R getList(@RequestParam(required=false) String key, @RequestParam(value="page",defaultValue="0")int page, @RequestParam(value="limit",defaultValue="5")int limit) throws SchedulerException {
        IPage<TbSsbusTaskEntrty> page1=tbSsbusTaskService.list(page,limit,key);
        return R.ok().put("page", page1);
    }

    /**
     *
     * @param tbSsbusTaskEntrty 添加定时器
     * @return
     * @throws SchedulerException
     */
    @PostMapping("/add")
    @SsbusLog(value = "定时任务_创建")
    @ApiOperation(value = "定时任务_创建", notes = "创建")
    @ResponseBody
    public R addQuartz(@RequestBody TbSsbusTaskEntrty tbSsbusTaskEntrty){
        tbSsbusTaskService.addQuartz(tbSsbusTaskEntrty);
        return R.ok();
    }

    /**
     *
     * @return 获取路由列表
     */
    @GetMapping("/gettbSsbusRouteList")
    @ResponseBody
    public R getTbSsbusRouteList(){
        List<TbSsbusRouteEntrty> list=tbSsbusRouteService.getTbSsbusRouteList();
        return R.ok().put("list",list);
    }

    /**
     *
     * @param id 任务id
     * @param faillog 是否开启失败日志
     * @return
     */
    @GetMapping("/upFaillog")
    @ResponseBody
    public R upFaillog(@RequestParam String id,@RequestParam Integer faillog){
        tbSsbusTaskService.upFaillog(id,faillog);
        return R.ok();
    }

    /**
     *
     * @param id 任务id
     * @param commonlog 是否开启常规日志
     * @return
     */
    @GetMapping("/upCommonlog")
    @ResponseBody
    public R upCommonlog(@RequestParam String id,@RequestParam Integer commonlog){
        tbSsbusTaskService.upCommonlog(id,commonlog);
        return R.ok();
    }

    /**
     *
     * @param tbSsbusTaskEntrty 修改定时任务
     * @return
     * @throws SchedulerException
     */
    @PostMapping("/update")
    @SsbusLog(value = "定时任务_修改")
    @ApiOperation(value = "定时任务_修改", notes = "修改")
    @ResponseBody
    public R update(@RequestBody TbSsbusTaskEntrty tbSsbusTaskEntrty) throws SchedulerException {
        tbSsbusTaskService.update(tbSsbusTaskEntrty);
        return R.ok();
    }

    /**
     * 开启任务
     * @param id
     * @return
     */
    @SsbusLog(value = "定时任务_开启")
    @RequestMapping(value = "start", method = RequestMethod.GET ,produces = "application/json;charset=UTF-8")
    public R save(@RequestParam(value = "id")String id){
        String result = tbSsbusTaskService.startTask(id);
        return  R.ok().put("data",result);
    }

    /**
     * 关闭任务
     * @param id
     * @return
     */
    @ApiOperation(value = "关闭任务", notes = "关闭任务")
    @RequestMapping(value = "stop", method = RequestMethod.GET ,produces = "application/json;charset=UTF-8")
    public R update(@RequestParam(value = "id")String id){
        String result = tbSsbusTaskService.stopTask(id);
        return  R.ok().put("data",result);
    }

    /**
     *
     * @param id
     * @return
     * @throws SchedulerException
     */
    @GetMapping("/getById/{id}")
    @ResponseBody
    public R getById(@PathVariable("id")  String id ) throws SchedulerException {
        TbSsbusTaskEntrty data=tbSsbusTaskService.getById(id);
        return R.ok().put("data",data);
    }

    /**
     *
     * @param id 根据id删除任务
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "定时任务_删除", notes = "删除")
    @SsbusLog(value = "定时任务_删除")
    @ResponseBody
    public R del(@RequestParam("id")  String id)  {
        tbSsbusTaskService.delete(id);
        return R.ok();
    }



    /**
     *
     * @param id 根据id批量删除
     * @return
     */
    @PostMapping("/deletes")
    public R deletes(@RequestBody String[] id) {
        tbSsbusTaskService.deletes(id);
        return R.ok();
    }

    /**
     *
     * @return 获取状态
     */
    @GetMapping("/status")
    public R status()  {
        List<TbBaseDictitemEntity> list=tbSsbusTaskService.status();
        return R.ok().put("list",list);
    }
}
